//app.js
var util = require('./utils/util.js')
var iFDU = require('./utils/iFDU.js')
App({
  onLaunch: function() {
    var that = this;
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    that.globalData.screenWidth = screenWidth;
    that.globalData.screenHeight = screenHeight;
  },

  getUserInfo: function (cb, param) {
    var that = this;
    if (that.globalData.userId > 0) {
      typeof cb == "function" && cb(that.globalData)
    } else {
      that.login(function (userId, userInfo) {
        var newUserData = {
          userId: userId,
          userInfo: userInfo,
          version: Date.now()
        };

        // wx.setStorageSync('userData', JSON.stringify(newUserData));
        that.globalData.userId = userId;
        that.globalData.userInfo = userInfo;

        typeof cb == "function" && cb(that.globalData)
      }, param ? param : undefined);
      // var userData = wx.getStorageSync('userData');
      // if (userData) {
      //   var userDataJson = JSON.parse(userData);
      //   if (Date.now() - userDataJson['version'] > 24 * 60 * 60 * 1000) {
      //     that.login(function (userId, userInfo) {
      //       var newUserData = {
      //         userId: userId,
      //         userInfo: userInfo,
      //         version: Date.now()
      //       };

      //       wx.setStorageSync('userData', JSON.stringify(newUserData));
      //       that.globalData.userId = userId;
      //       that.globalData.userInfo = userInfo;

      //       typeof cb == "function" && cb(that.globalData)
      //     });
      //   } else {
      //     that.globalData.userId = userDataJson.userId;
      //     that.globalData.userInfo = userDataJson.userInfo;

      //     typeof cb == "function" && cb(that.globalData)
      //   }
      // } else {
      //   that.login(function (userId, userInfo) {
      //     var newUserData = {
      //       userId: userId,
      //       userInfo: userInfo,
      //       version: Date.now()
      //     };
      //     wx.setStorageSync('userData', JSON.stringify(newUserData));
      //     that.globalData.userId = userId;
      //     that.globalData.userInfo = userInfo;

      //     typeof cb == "function" && cb(that.globalData)
      //   });
      // }
    }
  },

  login: function(cb, param) {
    var that = this;
    if (this.globalData.userId != 0) {
      typeof cb == "function" && cb(this.globalData.userId, this.globalData.userInfo)
    } else {
      wx.login({
        success: function (loginRes) {
          if (loginRes.code) {
            wx.getUserInfo({
              withCredentials: true,
              success: function (res) {
                console.log('Use info from weixin: ', res)
                var data = {
                  code: loginRes.code,
                  encryptedData: res.encryptedData,
                  iv: res.iv
                };
                if (param && param['channelId']) {
                  data['channelId'] = param['channelId'];
                }
                wx.request({
                  url: that.config.baseApiUrl + 'user/getAppUserInfo?channelId=21',

                  data: data,
                  success: function (result) {
                    if (result.statusCode == 200 && result.data && result.data.code == '0000') {
                      that.globalData.userId = result.data.data.userView.userid;
                      that.globalData.userInfo = res.userInfo;
                      typeof cb == "function" && cb(that.globalData.userId, that.globalData.userInfo)
                    } else {
                      
                    }
                  }
                })
              }
            })
          }
        }
      });
    }
  },

  config: iFDU.config,

  globalData: {
    userId: 0,
    userInfo: null,
    screenWidth: 0,
    screenHeight: 0
  }
})
